package com.leancloud.login.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.aries.ui.view.title.TitleBarView;
import com.leancloud.login.LoginPath;
import com.leancloud.login.R;
import com.leancloud.login.TimingTextView;
import com.leancloud.login.user.LoginBack;
import com.leancloud.login.user.UserGlobal;
import com.vise.xsnow.event.BusManager;

import tech.com.commoncore.ApplyName;
import tech.com.commoncore.LoginInterceptorImpl;
import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.event.LoginChangeEvent;
import tech.com.commoncore.utils.FastUtil;
import tech.com.commoncore.utils.RegUtils;
import tech.com.commoncore.utils.ToastUtil;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * 忘记密码
 */
@Route(path = LoginPath.LOGIN_RESETPWD,extras = LoginInterceptorImpl.LOGINCHECK)
public class ResetPwdActivity extends BaseTitleActivity implements View.OnClickListener {
    private EditText phoneEt, verifyEt, passwordEt, confirmEt;

    private TimingTextView timingTextView;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTextColor(Color.WHITE)
                .setLeftTextDrawable(null)
                .setBgColor(Color.TRANSPARENT)
                .setDividerHeight(0)
                .setStatusBarLightMode(true)
                .setRightTextDrawable(R.drawable.login_icon_back)
                .setOnRightTextClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_sms_find_password;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        ((TextView) findViewById(R.id.input_banner_tx)).setText(getResources().getString(R.string.regist_forget_pw_title));
        phoneEt = findViewById(R.id.login_input_phone_et);
        verifyEt = findViewById(R.id.login_input_verify_et);
        passwordEt = findViewById(R.id.login_input_password_et);
        confirmEt = findViewById(R.id.login_input_confirm_et);
        timingTextView = findViewById(R.id.login_input_verify_bt);

        findViewById(R.id.login_input_verify_bt).setOnClickListener(this);
        findViewById(R.id.btn_confirm).setOnClickListener(this);
//        findViewById(R.id.to_registered_tx).setOnClickListener(this);

        setToggleEffect();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_confirm) {
            resetPassword();
        }  else if (v.getId() == R.id.login_input_verify_bt) {
            requestVerificationCode();
        }

//        else if (v.getId() == R.id.to_registered_tx) {
//            ARouter.getInstance().build(PathManage.USER_PHONE_REGISTERED).greenChannel().navigation();
//        }

    }

    private void setToggleEffect() {
        ((ToggleButton) findViewById(R.id.password_toggle))
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        setPasswordShowOrHide(passwordEt, isChecked);
                    }
                });
        ((ToggleButton) findViewById(R.id.confirm_toggle))
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        setPasswordShowOrHide(confirmEt, isChecked);
                    }
                });
    }

    private void setPasswordShowOrHide(EditText editText, boolean checked) {
        if (checked) {
            editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
    }

    private boolean verifyNumber() {
        String phone = phoneEt.getText().toString().trim();
        if (phone == null || phone.isEmpty()) {
            ToastUtil.show(getResources().getString(R.string.regist_login_phone));
            return false;
        }
        if (!RegUtils.isMobile(phone)) {
            ToastUtil.show(getResources().getString(R.string.regist_phone));
            return false;
        }
        return true;
    }
    //获取验证码
    private void requestVerificationCode() {
//        String phone = phoneEt.getText().toString().trim();
//        if (!verifyNumber())
//            return;
//        BmobSMS.requestSMSCode(phone, "", new QueryListener<Integer>() {
//            @Override
//            public void done(Integer smsId, BmobException e) {
//                if (e != null) {
//                    ToastUtil.show(getResources().getString(R.string.regist_forget_code_fail));
//                }
//            }
//        });
        String phone = phoneEt.getText().toString().trim();
        if (!RegUtils.isMobile(phone))
            return;

        UserGlobal.getUserImp().requestAllVerificationCode(phone, ApplyName.AppName,ApplyName.ApplyName, new LoginBack() {
            @Override
            public void onBack(Object o, String err) {
                hideLoading();
                if (err == null) {
//                    BusManager.getBus().post(new LoginChangeEvent(true));
//                    finish();
                } else {
                    ToastUtil.show(getResources().getString(R.string.regist_code_fail));
                }
            }
        });
    }

    private void resetPassword() {
        String verify = verifyEt.getText().toString().trim();
        String password = passwordEt.getText().toString().trim();
        String confirm = confirmEt.getText().toString().trim();
        String phone = phoneEt.getText().toString().trim();

        if (phone == null || phone.isEmpty()) {
            ToastUtil.show(getResources().getString(R.string.regist_login_phone));
            return;
        }
        if (verify == null || verify.isEmpty()) {
            ToastUtil.show(getResources().getString(R.string.regist_code));
            return;
        }

        if (password == null || password.isEmpty()) {
            ToastUtil.show(getResources().getString(R.string.regist_new_pw));
            return;
        }

        if (confirm == null || confirm.isEmpty() || !confirm.equals(password)) {
            ToastUtil.show(getResources().getString(R.string.regist_pw_inconformity));
            return;
        }
        UserGlobal.getUserImp().weRetrievePassword(phone, password, verify,ApplyName.ApplyName, new LoginBack() {
            @Override
            public void onBack(Object o, String err) {
                hideLoading();
                if (err == null) {
                    FastUtil.startActivity(mContext, LoginActivity.class);
                    ResetPwdActivity.this.finish();
                    ToastUtil.show(getResources().getString(R.string.regist_forget_modify_successfully));
                } else {
                    ToastUtil.show(getResources().getString(R.string.regist_forget_fail_to_modify));
                }
            }
        });
//        BmobUser.resetPasswordBySMSCode(verify, password, new UpdateListener() {
//            @Override
//            public void done(BmobException e) {
//                hideLoading();
//                if (e == null) {
//                    ToastUtil.show(getResources().getString(R.string.regist_forget_modify_successfully));
////                    FastUtil.startActivity(mContext, LoginActivity.class);
//                    ResetPwdActivity.this.finish();
//                } else {
//                    ToastUtil.show(getResources().getString(R.string.regist_forget_fail_to_modify));
//                }
//            }
//        });
    }
}
